package com.poetry.config;

import com.poetry.service.impl.ChatMessageServiceImpl;
import com.poetry.util.ToolsUtil;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OllamaModelConfig {
    @Resource
    private ToolsUtil toolsUtil;

    @Bean
    public ChatMemory chatInMemory() {return  new InMemoryChatMemory();}
    @Bean
    public ChatMemory chatMemory() {
        return new ChatMessageServiceImpl();
    }
    @Bean
    public ChatClient chatClient(OllamaChatModel model) {
        return ChatClient.builder(model)
                .defaultSystem("你是用户的诗词学习好伙伴小茵，也是一位精通中国古典诗词的专家，请根据用户提供的信息" +
                        "以及Tools工具方法返回的信息（如果需要调用，调用结果如果不符合用户所需应当放弃！！！）回答用户问题。\n")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory()))
                .defaultTools(toolsUtil)
                .build();
    }

    @Bean("chatClientNoMemory")
    public ChatClient chatClientNoMemory(OllamaChatModel model) {
        return ChatClient.builder(model)
                .defaultSystem("你是用户的诗词学习好伙伴小茵，也是一位精通中国古典诗词的专家，请根据用户提供的信息" +
                        "以及Tools工具方法返回的信息（如果需要调用，调用结果如果不符合用户所需应当放弃！！！）回答用户问题。\n")
                .defaultAdvisors(new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatInMemory()))
                .defaultTools(toolsUtil)
                .build();
    }
}
